#region Copyright

// // The contents of this file are subject to the Mozilla Public License
// // Version 1.1 (the "License"); you may not use this file except in compliance
// // with the License. You may obtain a copy of the License at
// //   
// // http://www.mozilla.org/MPL/
// //   
// // Software distributed under the License is distributed on an "AS IS"
// // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
// // License for the specific language governing rights and limitations under 
// // the License.
// //   
// // The Initial Developer of the Original Code is Robert Smyth.
// // Portions created by Robert Smyth are Copyright (C) 2008-2013.
// //   
// // All Rights Reserved.

#endregion

namespace NoeticTools.NWorkflow.ProgressMonitoring
{
    public class WorkflowProgressMonitor<T> : IProgress
    {
        private readonly IProgressScopeService _scopeService;
        private IProgress _inner;

        internal WorkflowProgressMonitor(IProgressScopeService scopeService)
        {
            _scopeService = scopeService;
            ProgressAvailable = true;
            Message = string.Empty;
        }

        public bool ProgressAvailable { get; private set; }

        public double UpdateWeights(double startingWeight)
        {
            StartingWeight = startingWeight;
            startingWeight = _scopeService.UpdateWeights(startingWeight);
            Total = startingWeight - StartingWeight;
            return startingWeight;
        }

        public IProgress Inner
        {
            get { return _inner; }
            set
            {
                _inner = value;
                UpdateCompleted();
            }
        }

        public double Total { get; set; }

        public string Message { get; set; }

        public double AccumulatedCompleted
        {
            get
            {
                if (!double.IsNaN(Completed) && Total > 0.0)
                {
                    return StartingWeight + (Completed*Total);
                }

                //if (Inner != null)
                //{
                //    return Inner.AccumulatedCompleted;
                //}
                //if (_workflowRunner.CurrentStep != null)
                //{
                //    return _workflowRunner.CurrentStep.Progress.AccumulatedCompleted;
                //}
                return StartingWeight + Total;
            }
        }

        public double Completed { get; set; }
        public double StartingWeight { get; set; }

        private void UpdateCompleted()
        {
            //Completed = Total > 0.0 ? (AccumulatedCompleted - StartingWeight)/Total : 0.0;
        }
    }
}